clear
set more off
set type double
graph drop _all
program drop _all
set scheme s2mono
log using "\\file\UsersW$\wrr15\Home\My Documents\My Files\EFFECTS OF SPILLOVERS ON EXPORTS_A META-ANALYSIS\DATAVERSE FILES\Table9Results.smcl", replace
use "\\file\UsersW$\wrr15\Home\My Documents\My Files\EFFECTS OF SPILLOVERS ON EXPORTS_A META-ANALYSIS\DATAVERSE FILES\meta-orig.dta", clear

rename spill_other_firm all
rename idstudy id
rename sameregion spill_reg
rename sameindustry spill_ind
rename spill_exporter spill_ex
rename spill_value value
rename spill_employment employment
rename spill_output output
rename spill_number number
rename spill_asset asset
rename spill_rd rdsp
rename spill_other other_ms
rename olsgls ogls
rename probit plt
rename nonspherical nsph
rename endogeneity endg
rename categorical catg
rename sampleselection sps
rename panel_fe fe
rename aggregate agg
rename crosssection cs
rename labourquality labq
rename productivity prod
rename publicationyear pubyear

replace all = 1 if foreign ==1
drop foreign
rename all nondomestic
replace other_ms = 1 if asset==1
drop asset
replace impact = 0 if impact ==.
gen firm_level = 1 if agg == 0
replace firm_level = 0 if agg == 1

******************************************************************************
*DEFINITION OF BASIC VARIABLES
******************************************************************************
gen tstat = t
gen df = samplesize - k
gen r = tstat/sqrt(tstat^2+df)
gen varR = (1-r^2)/df
gen seR = sqrt(varR)
gen pcc = r
gen avgyear=(end+start)/2
gen tspan= end-start+1
gen abststat = abs(tstat)
gen abspcc = abs(pcc)

******************************************************************************
* Kick out extreme values of PCC
******************************************************************************
quietly summ pcc, detail
keep if pcc > r(p1) & pcc < r(p99) 

// Calculating study weights
bysort id: egen numberests = count(id)
tabulate numberests 
gen weight = 1/numberests

// Generating transformed variables for FE and RE
gen fetstatR = r/seR
metareg r seR , wsse(seR)
scalar tau2 =  e(tau2)
gen revarR = varR + tau2
gen reseR = sqrt(revarR)
gen retstatR = r/reseR
gen repubbiasR = seR/reseR


**********************************************
************ FIXED EFFECTS *******************
**********************************************

******************************************************************************
// This section transforms the continuous variables so they are 
// deviations from the mean.  This makes the constant more "interpretable".
gen conss = 1/seR
gen spill_exx = spill_ex/seR
gen spill_regg = spill_reg/seR
gen spill_indd = spill_ind/seR
gen spill_fdii = spill_fdi/seR
gen numberr = number/seR
gen valuee = value/seR
gen employmentt = employment/seR
gen manufacturingg = manufacturing/seR
gen avgyearr = avgyear/seR
gen firm_levell = firm_level/seR
gen domesticc = domestic/seR
gen oecdd = oecd/seR
gen chinaa = china/seR
gen binaryy = binary/seR
gen pltt = plt/seR
gen endgg = endg/seR
gen spss = sps/seR
gen fee = fe/seR
gen sizee = size/seR
gen prodd = prod/seR
gen labqq = labq/seR
gen capitall = capital/seR
gen rdd = rd/seR
gen publishedd = published/seR
gen impactt = impact/seR
gen citationn = citation/seR


// The "All Control Variables" specifications include the SeR term
// NOTE: The constant term is the SER term

//////////////////////////////////////////////
//////////////  FIXED EFFECTS  ///////////////
//////////////////////////////////////////////

// THIS SECTION GIVES EQUAL WEIGHT TO EACH OBSERVATION

*--------------------------------------------------------*
*   TABLE: FixedEffects(Weight1) // Stepwise Regression  *
*--------------------------------------------------------*

// Here we use backwards stepwise regression selecting on the best regression using the BIC criterion
// We lock in the 4 spillover variables and the conss variable: Spill_ind Spill_reg Spill_FDI Spill_ex 
vselect fetstatR ///
avgyearr firm_levell domesticc oecdd ///
chinaa binaryy manufacturingg pltt endgg spss fee sizee prodd labqq capitall rdd ///
publishedd impactt citationn , backward bic ///
fix(conss spill_exx spill_regg spill_indd spill_fdii numberr valuee employmentt)

reg fetstatR conss spill_exx spill_regg spill_indd spill_fdii numberr ///
valuee employmentt avgyearr firm_levell ///
chinaa binaryy endgg fee capitall rdd ///
publishedd  , vce(cluster id) 
display e(r2_a)

// THIS SECTION GIVES EQUAL WEIGHT TO EACH STUDY

*--------------------------------------------------------*
*   TABLE: FixedEffects(Weight2) // Stepwise Regression  *
*--------------------------------------------------------*

// Here we use backwards stepwise regression selecting on the best regression using the BIC criterion
// We lock in the 4 spillover variables and the conss variable: Spill_ind Spill_reg Spill_FDI Spill_ex 
vselect fetstatR  ///
avgyearr firm_levell domesticc oecdd ///
chinaa binaryy manufacturingg pltt endgg spss fee sizee prodd labqq capitall rdd ///
publishedd impactt citationn [pweight = weight] , backward bic ///
fix(conss spill_exx spill_regg spill_indd spill_fdii numberr valuee employmentt)


reg fetstatR conss spill_exx spill_regg spill_indd spill_fdii numberr ///
valuee employmentt avgyearr firm_levell  ///
chinaa binaryy pltt fee capitall rdd ///
publishedd impactt citationn [pweight = weight], vce(cluster id)
display e(r2_a)


///////////////////////////////////////////////
//////////////  RANDOM EFFECTS  ///////////////
///////////////////////////////////////////////

replace conss = 1/reseR
replace spill_exx = spill_ex/reseR
replace spill_regg = spill_reg/reseR
replace spill_indd = spill_ind/reseR
replace spill_fdii = spill_fdi/reseR
replace numberr = number/reseR
replace valuee = value/reseR
replace employmentt = employment/reseR
replace manufacturingg = manufacturing/reseR
replace avgyearr = avgyear/reseR
replace firm_levell = firm_level/reseR
replace domesticc = domestic/reseR
replace oecdd = oecd/reseR
replace chinaa = china/reseR
replace binaryy = binary/reseR
replace pltt = plt/reseR
replace endgg = endg/reseR
replace spss = sps/reseR
replace fee = fe/reseR
replace sizee = size/reseR
replace prodd = prod/reseR
replace labqq = labq/reseR
replace capitall = capital/reseR
replace rdd = rd/reseR
replace publishedd = published/reseR
replace impactt = impact/reseR
replace citationn = citation/reseR



// THIS SECTION GIVES EQUAL WEIGHT TO EACH OBSERVATION


*---------------------------------------------------------*
*   TABLE: RandomEffects(Weight1) // Stepwise Regression  *
*---------------------------------------------------------*

// NOTE: For Random Effects models, SE/reseR is not equal to 1. This causes a problem because the
// stepwise regression command does not have a "noconstant" option. Our solution is go with the constant
// as a proxy for SE/reseR in choosing the control variables, but then when it comes time to estimate
// the final equation, then drop the constant and replace with the variable PubBias, which = SE/reseR.

// Here we use backwards stepwise regression selecting on the best regression using the BIC criterion
// We lock in the 4 spillover variables and the conss variable: Spill_ind Spill_reg Spill_FDI Spill_ex 
vselect retstatR repubbiasR  ///
avgyearr firm_levell domesticc oecdd ///
chinaa binaryy manufacturingg pltt endgg spss fee sizee prodd labqq capitall rdd ///
publishedd impactt citationn , backward bic ///
fix(conss spill_exx spill_regg spill_indd spill_fdii numberr valuee employmentt)

reg retstatR conss repubbiasR spill_exx spill_regg spill_indd spill_fdii numberr ///
valuee employmentt avgyearr domesticc oecdd ///
chinaa pltt spss fee labqq capitall rdd ///
impactt  , noc vce(cluster id)
display e(r2_a)

// THIS SECTION GIVES EQUAL WEIGHT TO EACH STUDY

*---------------------------------------------------------*
*   TABLE: RandomEffects(Weight2) // Stepwise Regression  *
*---------------------------------------------------------*

// NOTE: For Random Effects models, SE/reseR is not equal to 1. This causes a problem because the
// stepwise regression command does not have a "noconstant" option. Our solution is go with the constant
// as a proxy for SE/reseR in choosing the control variables, but then when it comes time to estimate
// the final equation, then drop the constant and replace with the variable PubBias, which = SE/reseR.

// Here we use backwards stepwise regression selecting on the best regression using the BIC criterion
// We lock in the 4 spillover variables and the conss variable: Spill_ind Spill_reg Spill_FDI Spill_ex 
vselect retstatR repubbiasR  ///
avgyearr firm_levell domesticc oecdd ///
chinaa binaryy manufacturingg pltt endgg spss fee sizee prodd labqq capitall rdd ///
publishedd impactt citationn [pweight = weight] , backward bic ///
fix(conss spill_exx spill_regg spill_indd spill_fdii numberr valuee employmentt)


reg retstatR conss repubbiasR spill_exx spill_regg spill_indd spill_fdii numberr ///
valuee employmentt avgyearr  ///
chinaa pltt spss fee prodd labqq capitall rdd ///
impactt [pweight = weight], noc vce(cluster id)
display e(r2_a)


log close

 